<?php


namespace App\Models;


use antcloud\AntCloudSDKCore\AddressClient;
use App\Services\ZlbService;
use Illuminate\Database\Eloquent\Model;

class ZlbOrder extends Model
{
    protected $table = "order";
    protected $primaryKey = 'id';
    public $timestamps = false;

    /**
     * 订单上链新增数据
     * @param $order
     * @param $lease
     * @param $buyMoney
     * @param $request
     * @param $orderGoods
     * @param $filePath
     */
    public static function add($order,$lease,$buyMoney,$request,$orderGoods,$filePath){
        $data = [
            'shop_id'=>$order['seller_id'],
            'order_no'=>$order['trade_no'],
            'rent_total'=>$order['rent_total'],
            'month_total'=>$order['month_total'],
            'relet_rent_total'=>$buyMoney,
            'pay_trade_no'=>$lease['id'],
            'pay_type'=>$lease['pay_type'] == 2 ? 1 : 3,
            'pay_time'=>dateFormat($lease['pay_time']),
            'foregift'=>!empty($order['foregift']) ? $order['foregift'] : 0,
            'foregift_deduct'=>!empty($order['foregift_deduct']) ? $order['foregift_deduct'] : 0,
            'create_time'=>dateFormat($order['create_time']),
            'flow_id'=>$order['flow_id'],
            'pact_url'=>$order['pact_url'],
            'contract_hash'=>$filePath,
            'setmeal_id'=>$orderGoods['setmeal_id'],
        ];
        ZlbOrder::insertGetId($data);
        //添加收货地址
        $code = AddressClient::getArea($request['province'],$request['city'],$request['area']);
        $logic = [
            'order_no'=>$order['trade_no'],
            'prov_code'=>$code['provinceCode'],
            'city_code'=>$code['cityCode'],
            'area_code'=>$code['areaCode'],
            'address'=>$order['address']
        ];
        ZlbLogistics::insertGetId($logic);
    }

    /**
     * 订单上链
     * @param $order
     * @param $request
     * @param $notary
     */
    public static function set($order,$request,$notary){
        $res = ZlbService::format($notary);
        if($res['status'] == ZlbService::STATUS_YES){
            //成功
            ZlbService::requestLog('上传订单成功',$request,$res);
        }else{
            //失败
            ZlbService::requestLog('上传订单失败',$request,$res);
        }
        $data = [
            'tx_hash'=>$res['tx_hash'],
            'chain_status'=>$res['status'],
            'chain_time'=>dateFormat(time()),
            'chain_params'=>ZlbService::paramsResponse($request,$notary),
        ];
        ZlbOrder::where(['order_no'=>$order['trade_no']])->update($data);
    }

    /**
     * 订单承诺上链
     * @param $order
     * @param $request
     * @param $notary
     */
    public static function promiseSet($order,$request,$notary){
        $res = ZlbService::format($notary);
        if($res['status'] == ZlbService::STATUS_YES){
            //成功
            ZlbService::requestLog('上传订单承诺信息成功',$request,$res);
        }else{
            //失败
            ZlbService::requestLog('上传订单承诺信息失败',$request,$res);
        }
        $data = [
            'promise_tx_hash'=>$res['tx_hash'],
            'promise_chain_status'=>$res['status'],
            'promise_chain_time'=>dateFormat(time()),
            'promise_chain_params'=>json_encode($notary),
        ];
        ZlbOrder::where(['order_no'=>$order['trade_no']])->update($data);
    }

    public function lease(){
        return $this->belongsToMany('App\Models\ZlbOrderLease', 'order_lease');
    }


}
